//英魂战场
package battleGround

import (
	"math/rand"
	"sanguo/node/common/com_type"
	table_BattleGround "sanguo/node/table/excel/table/BattleGround"
)

const (
	No_Enter         = 1 //战役没有进入
	Enter_Not_Finish = 2 //进入战役但没有完成
	Enter_And_Finish = 3 //完成战役
)

type BattleGround struct {
	BattleGroundID      int32
	Count               int32 //进入的战役计数
	CurrentEnter        int32 //当前进入的战役
	ExplorePower        int32 //探索倍率
	FreeResurgenceTimes int32 //免费复活次数
	BattleLevelMap      map[int32]*BattleLevel
	ThroughDropPool     int32
	OldMapID            int32
	OldPosX             int32
	OldPosY             int32
	OldPosZ             int32
	OldPosAngle         int32
	IsFailed            bool
}

type BattleLevel struct {
	IndexID       int32
	BattleLevelID int32
	Degree        int32
	BuffID        int32
	State         int32
}

//初始化战场的关卡ID
func (this *BattleGround) InitBattleLevel(ID int32) {
	BGDef := table_BattleGround.Table_.IDMap[ID]
	count := BGDef.Count
	blMap := map[int]*BattleLevel{}

	//boos关
	var bossCount int32 = 0
	if BGDef.BoosMaxCount != 0 {
		bossCount = rand.Int31n(BGDef.BoosMaxCount) + 1
	}
	for i := 0; i < int(bossCount); i++ {
		var blID int32
		if BGDef.BoosRangeRight-BGDef.BoosRangeLeft != 0 {
			blID = rand.Int31n(BGDef.BoosRangeRight-BGDef.BoosRangeLeft+1) + BGDef.BoosRangeLeft
		} else {
			blID = BGDef.BoosRangeLeft
		}
		blMap[i] = &BattleLevel{
			BattleLevelID: blID,
			Degree:        com_type.YhBattleGroundDegree_Boss,
		}
	}

	//精英关
	var eliteCount int32 = 0
	if BGDef.EliteMaxCount != 0 {
		eliteCount = rand.Int31n(BGDef.EliteMaxCount) + 1
	}
	for i := 0 + 10; i < int(eliteCount)+10; i++ {
		var blID int32
		if BGDef.EliteRangeRight-BGDef.EliteRangeLeft != 0 {
			blID = rand.Int31n(BGDef.EliteRangeRight-BGDef.EliteRangeLeft+1) + BGDef.EliteRangeLeft
		} else {
			blID = BGDef.EliteRangeLeft
		}
		blMap[i] = &BattleLevel{
			BattleLevelID: blID,
			Degree:        com_type.YhBattleGroundDegree_Elite,
		}
	}

	//普通关
	GeneralCount := count - bossCount - eliteCount
	for i := 0 + 20; i < int(GeneralCount)+20; i++ {
		var blID int32
		if BGDef.GeneralRangeRight-BGDef.GeneralRangeLeft != 0 {
			blID = rand.Int31n(BGDef.GeneralRangeRight-BGDef.GeneralRangeLeft+1) + BGDef.GeneralRangeLeft
		} else {
			blID = BGDef.GeneralRangeLeft
		}
		blMap[i] = &BattleLevel{
			BattleLevelID: blID,
			Degree:        com_type.YhBattleGroundDegree_General,
		}
	}

	var index int32 = 1
	for id, v := range blMap {
		this.BattleLevelMap[index] = &BattleLevel{
			IndexID:       index,
			BattleLevelID: v.BattleLevelID,
			Degree:        v.Degree,
			State:         No_Enter,
		}
		if len(BGDef.BuffID) == 0 {
			this.BattleLevelMap[index].BuffID = 0
		} else {
			buffId := rand.Intn(len(BGDef.BuffID))
			this.BattleLevelMap[index].BuffID = BGDef.BuffID[buffId].BuffId
		}
		index++
		delete(blMap, id)
	}
}

func InitBattleGround(battleGroundID, power int32, mapID, x, y, z, angle int32) *BattleGround {
	BattleGroundDef := table_BattleGround.Table_.IDMap[battleGroundID]

	battleGround := &BattleGround{
		BattleGroundID:      battleGroundID,
		Count:               int32(0),
		ExplorePower:        power,
		BattleLevelMap:      map[int32]*BattleLevel{},
		ThroughDropPool:     BattleGroundDef.ThroughDropPool,
		FreeResurgenceTimes: 0,
		OldMapID:            mapID,
		OldPosX:             x,
		OldPosY:             y,
		OldPosZ:             z,
		OldPosAngle:         angle,
		IsFailed:            false,
	}

	battleGround.InitBattleLevel(battleGroundID)
	return battleGround
}

//进入战役
func (this *BattleGround) EnterCampaign(index int32) bool {
	battleLevel := this.BattleLevelMap[index]
	if battleLevel == nil {
		return false
	}

	if battleLevel.State != No_Enter {
		return false
	}
	battleLevel.State = Enter_Not_Finish
	this.CurrentEnter = index
	this.Count++
	return true
}

//结束战役
func (this *BattleGround) ExitCampaign(b bool) bool {
	battleLevel := this.BattleLevelMap[this.CurrentEnter]
	if battleLevel == nil {
		return false
	}

	if battleLevel.State != Enter_Not_Finish {
		return false
	}
	if b {
		battleLevel.State = Enter_And_Finish
		if battleLevel.Degree == com_type.YhBattleGroundDegree_Boss || battleLevel.Degree == com_type.YhBattleGroundDegree_Elite {
			this.FreeResurgenceTimes++
		}
	} else {
		this.IsFailed = true
	}
	this.CurrentEnter = 0
	return true
}

func (this BattleGround) GetExplorePower() int32 {
	return this.ExplorePower
}

//最后一场战役 且前面的战役都已通关
func (this *BattleGround) IsFinally() bool {
	BattleGroundDef := table_BattleGround.Table_.IDMap[this.BattleGroundID]
	if this.Count == BattleGroundDef.Count {
		return true
	}
	return false
}

func (this *BattleGround) GetBattleLevelByIndex(index int32) *BattleLevel {
	return this.BattleLevelMap[index]
}

//使用复活
func (this *BattleGround) UseFreeResurgenceTimes() bool {
	if this.FreeResurgenceTimes <= 0 {
		return false
	}
	this.FreeResurgenceTimes--
	return true
}
